using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

using ObjectLibrary;
namespace AIProcessor
{
    abstract public class ComputerControl
    {
        protected List<Vector3> specialPoint;
        Vector3 TargetPosition;
        protected Fighter _fighter;
        protected int _team;
        protected Random randomEngine = new Random();
        protected int fireTime = 0;
        protected int startFireTime = 100;
        protected Fighter _targetFoe;
        virtual public void UpdateAction(Word outsideWord,Game game)
        {

        }
        virtual protected float GetFitnessValue(Fighter fighter,List<Fighter> listFriends)
        {
            Vector3 dPos = fighter.CurrentPosition - _fighter.CurrentPosition;
            float d = dPos.Length();
            float distantPoint = 200 / (d + 1);
            float friendsPoint = 0;
            for (int i = 0; i < listFriends.Count; i++)
            {
                if (listFriends[i]!= _fighter && (listFriends[i].CurrentPosition - fighter.CurrentPosition).Length() < 10)
                    friendsPoint -= 20;
            }
            return distantPoint + friendsPoint;
        }
        virtual protected float GetFireFitnessValue(Fighter fighter, List<Fighter> listFriends)
        {
            Vector3 dPos = fighter.CurrentPosition - _fighter.CurrentPosition;
            float d = dPos.Length();
            float distantPoint = -(d - 30)*(d - 30) + 100;
            float friendsPoint = 0;
            for (int i = 0; i < listFriends.Count; i++)
            {
                if (listFriends[i] != _fighter && (listFriends[i].CurrentPosition - fighter.CurrentPosition).Length() < 10)
                    friendsPoint -= 20;
            }
            int temp = fireTime;
            if (temp < 100)
                temp = 100;
            float ppPoint = (_fighter.Pp - Fighter.MAX_PP / 2) * 2 + (temp - 50) / 2;

            return distantPoint + friendsPoint + ppPoint;
        }
        virtual protected Fighter GetNewTarget(Word outsideWord)
        {
            Fighter targetFoe = null;
            float maxValue = float.MinValue;
            int type = 0;
            for (int i = 0; i < outsideWord.ListPlayers.Count; i++)
            {
                if (i != _team)
                {
                    foreach (Fighter fighter in outsideWord.ListPlayers[i])
                    {
                        if (fighter.CurrentState != Fighter.State.dead || (fighter.CurrentPosition - _fighter.CurrentPosition).Length() > 100)
                        {
                            float value = GetFitnessValue(fighter, outsideWord.ListPlayers[_team]);
                            float fireValue = GetFireFitnessValue(fighter, outsideWord.ListPlayers[_team]);
                            if (value > maxValue)
                            {
                                targetFoe = fighter;
                                maxValue = value;
                                type = 0;
                            }
                            if (fireValue > maxValue)
                            {
                                targetFoe = fighter;
                                maxValue = fireValue;
                                type = 1;
                            }
                        }
                    }
                }
            }
            return targetFoe;
        }
        public bool Moveto(Vector3 pos, float radius)
        {
            
            Vector3 dPos = pos - _fighter.CurrentPosition;
            float d = dPos.Length();
            _fighter.LookRotationY = _fighter.GetVecterRotation(dPos);
            if (d <= radius)
            {
                return true;
            }
            if (Fighter.getDeltaRotaionY(_fighter.LookRotationY,_fighter.RotationY) < MathHelper.PiOver4 )
            {
               
                _fighter.Run();
            }
            else
            {

                _fighter.Idle();
            }
            return false;
        }
        /*protected void AutoSetSpecialPoint()
        {

        }*/
        int time = 0;
        protected void RandomAcction()
        {
            time++;
            if (randomEngine.Next(200) < time)
            {
                _fighter.LookRotationY += (float)randomEngine.Next(8) / 8 * MathHelper.Pi - MathHelper.PiOver2;
            }
            if (randomEngine.Next(2000) < time)
            {
                time = 0;
                if (randomEngine.Next(3) > 0)
                {
                    _fighter.Run();
                }
                else
                {
                    _fighter.Idle();
                }
            }

        }

    }
}
